using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI;
using S7.Net;
using System.Drawing.Printing;
using System.Dynamic;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using FreeSql;
using AntdUI;
using System.Collections;
using MiniExcelLibs;
using System.Web;
using Microsoft.IdentityModel.Tokens;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml;
using System.Diagnostics;
using Newtonsoft.Json.Linq;

//创建时间：2024-10-08
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// PLC配置表
    /// </summary>
    [Verify]
    [Route("business/plcconfig")]
    [ApiExplorerSettings(GroupName = "business")]
    public class PlcConfigController : BaseController
    {
        /// <summary>
        /// PLC配置表接口
        /// </summary>
        private readonly IPlcConfigService _plcConfigService;

        public PlcConfigController(IPlcConfigService PlcConfigService)
        {
            _plcConfigService = PlcConfigService;
        }

        /// <summary>
        /// 查询PLC配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "device:plcconfig:list")]
        public IActionResult QueryPlcConfig([FromQuery] PlcConfigQueryDto parm)
        {
            var response = _plcConfigService.GetList(parm);

            return SUCCESS(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("productionLinePlcInfo")]
        public IActionResult QueryProductionLinePlcInfo() {

            var list = _plcConfigService.GetAll().ToList();
            var response = list.GroupBy(o => new { o.ProductionLineCode })
                .Select(o => new { o.Key.ProductionLineCode })
                .ToList();

            JArray jArray = new JArray();
            foreach (var item in response) {
                JObject keys = new JObject();
                keys.Add("lineCode", item.ProductionLineCode);
                JArray values = JArray.FromObject(list.Where(o => o.ProductionLineCode == item.ProductionLineCode).ToList());
                keys.Add("workStation", values);
                jArray.Add(keys);
            }
            return SUCCESS(jArray);
        }

        /// <summary>
        /// 查询PLC配置表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "device:plcconfig:query")]
        public IActionResult GetPlcConfig(int Id)
        {
            var response = _plcConfigService.GetInfo(Id);

            var info = response.Adapt<PlcConfigDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加PLC配置表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "device:plcconfig:add")]
        [Log(Title = "PLC配置表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddPlcConfig([FromBody] PlcConfigDto parm)
        {
            var modal = parm.Adapt<PlcConfig>().ToCreate(HttpContext);

            var response = _plcConfigService.AddPlcConfig(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新PLC配置表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "device:plcconfig:edit")]
        [Log(Title = "PLC配置表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdatePlcConfig([FromBody] PlcConfigDto parm)
        {
            var modal = parm.Adapt<PlcConfig>().ToUpdate(HttpContext);
            var response = _plcConfigService.UpdatePlcConfig(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除PLC配置表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "device:plcconfig:delete")]
        [Log(Title = "PLC配置表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeletePlcConfig([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_plcConfigService.Delete(idArr));
        }
    }
}